//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCUtils {
    private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/javawebdb";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    public JDBCUtils() {
    }

    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            return DriverManager.getConnection("jdbc:mysql://localhost:3306/javawebdb", "root", "root");
        } catch (SQLException | ClassNotFoundException var1) {
            throw new RuntimeException("获取数据库连接失败", var1);
        }
    }

    public static void closeResources(Connection conn, PreparedStatement pstmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }

            if (pstmt != null) {
                pstmt.close();
            }

            if (conn != null) {
                conn.close();
            }

        } catch (SQLException var4) {
            throw new RuntimeException("关闭资源失败", var4);
        }
    }
    public static int executeUpdate(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);

            int i;
            for(i = 0; i < params.length; ++i) {
                pstmt.setObject(i + 1, params[i]);
            }

            i = pstmt.executeUpdate();
            System.out.println(i);
            return i;
        } catch (Exception var8) {
            throw new RuntimeException("执行更新操作失败", var8);
        } finally {
            closeResources(conn, pstmt, (ResultSet)null);
        }
    }

    public static ResultSet executeQuery(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);

            for(int i = 0; i < params.length; ++i) {
                pstmt.setObject(i + 1, params[i]);
            }

            return pstmt.executeQuery();
        } catch (Exception var5) {
            throw new RuntimeException("执行查询操作失败", var5);
        }
    }
}
